﻿using System.Security.Cryptography;
using AutoMapper;
using MediatR;
using Mysqlx.Crud;
using Return.Enum;
using SlaughterItem.Common.Infrastructures.Interfaces;
using THE_WIND_RISES.Common.Domans.BasicInformation.Materials;
using THE_WIND_RISES.Common.Domans.Produce.ManifestInfos;
using THE_WIND_RISES.Common.Dtos;

namespace Production_Set.API.MaterialManagement.Add
{
    public class BillMaterialCreateHandler : IRequestHandler<BillMaterialCreateCommon, Result>
    {
        private readonly IBaseRepository<ItemManifestInfo> ItemManifestInfoRepository;
        public IMapper mapper { get; set; }

        public BillMaterialCreateHandler(IBaseRepository<ItemManifestInfo> itemManifestInfoRepository, IMapper mapper)
        {
            ItemManifestInfoRepository = itemManifestInfoRepository;
            this.mapper = mapper;
        }



        public async Task<Result> Handle(BillMaterialCreateCommon request, CancellationToken cancellationToken)
        {
            var result = new Result() { Code = (int)CommonErrorCodes.Added };
            var AddData = mapper.Map<ItemManifestInfo>(request);
            string sql = "insert into ItemManifestInfo values(@ItemId,@ItemManifestCode,@ItemManifestVersion,@ChooseId,@ItemDesc,@ItemUnit,@ItemSpecs,@SheetPeople,@DepartmentId,@ItemState,@IsDelete,@Handle,@HandleContext,@HandleTime,@Pid);";
            var AddNum = ItemManifestInfoRepository.Add(sql, new
            {
                AddData.ItemId,
                AddData.ItemManifestCode,
                AddData.ItemManifestVersion,
                AddData.ChooseId,
                AddData.ItemDesc,
                AddData.ItemUnit,
                AddData.ItemSpecs,
                request.SheetPeople,
                AddData.DepartmentId,
                AddData.ItemState,
                AddData.IsDelete,
                AddData.Handle,
                AddData.HandleContext,
                AddData.HandleTime,
                AddData.Pid,
            });
            if (AddNum > 0)
            {
                result.Code = (int)CommonErrorCodes.Success;
                return result;
            }
            else
            {
                return result;
            }
        }
    }
}
